<?php
/**
 * Description of Util
 *
 * @author Magno
 */
class Util {

    public static function searchPosWordInBlankSpace($string){
        for ($i = 0; $i < strlen($string); $i++) {
            if(strcmp($string, " ") != 0)
                return $i;
        }
        return -1;
    }

    public static function isType($types, $type){
        $type = strtoupper(trim($type));
        for ($i = 0; $i < count($types); $i++) {
            if(stripos($types[$i], $type))
                return true;
        }
        return false;
    }

    public static function isTypeNumeric($type){
        $types = array("#INT","DECIMAL","DOUBLE","FLOAT","NUMERIC");
        return Util::isType($types, $type);
    }

    public static function isTypeText($type){
        $types = array("CHAR","TEXT");
        return Util::isType($types, $type);
    }

    public static function isTypeTime($type){
        $types = array("DATE","TIME");
        return Util::isType($types, $type);
    }

    public static function isTypeEspecial($type){
        $types = array("#ARRAY","#OBJECT");
        return Util::isType($types, $type);
    }

    public static function getInitValue($type, $default, $isFK){
        $value = '""';
        if(Util::isTypeNumeric($type)){
            $value = "-1";
            if(strlen($default))
                $value = $default;
        }else if(Util::isTypeText($type) || Util::isTypeTime($type)){
            $value = "\"\"";
        }

        if($isFK || Util::isTypeEspecial($type)){
            $value = "null";
        }

        return $value;
    }

    public static function xmlHighlight($s){       
        $s = htmlspecialchars($s);
        $s = preg_replace("#&lt;([/]*?)(.*)([\s]*?)&gt;#sU",
            "<font color=\"blue\">&lt;\\1\\2\\3&gt;</font>",$s);
        $s = preg_replace("#&lt;([\?])(.*)([\?])&gt;#sU",
            "<font color=\"#800000\">&lt;\\1\\2\\3&gt;</font>",$s);
        $s = preg_replace("#&lt;([^\s\?/=])(.*)([\[\s/]|&gt;)#iU",
            "&lt;<font color=\"blue\">\\1\\2</font>\\3",$s);
        $s = preg_replace("#&lt;([/])([^\s]*?)([\s\]]*?)&gt;#iU",
            "&lt;\\1<font color=\"blue\">\\2</font>\\3&gt;",$s);
        $s = preg_replace("#([^\s]*?)\=(&quot;|')(.*)(&quot;|')#isU",
            "<font color=\"red\">\\1</font>=<font color=\"purple\">\\2\\3\\4</font>",$s);
        $s = preg_replace("#&lt;(.*)(\[)(.*)(\])&gt;#isU",
            "&lt;\\1<font color=\"red\">\\2\\3\\4</font>&gt;",$s);
        $s = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", $s);

        return nl2br($s);
    }
}
?>
